package com.ym.controller.management;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.ym.core.domain.APIResponse;
import com.ym.core.domain.BaseController;
import com.ym.core.exception.BusinessException;
import com.ym.core.shiro.ShiroUtil;

import com.ym.medical.domain.entity.HealthCoinSourceSettingEntity;
import com.ym.medical.domain.param.HealthCoinSourceSettingParam;
import com.ym.medical.service.IHealthCoinSourceSettingSV;
import com.ym.medical.service.IUserHealthRecordSV;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@Api(tags = "健康币管理中心")
@Controller
@RequestMapping("/management/conf/healthCoin")
public class HealthCoinManagement extends BaseController {
	private static Logger logger = LoggerFactory.getLogger(HealthCoinManagement.class);

	@Autowired
	private IUserHealthRecordSV userHealthRecordSV;

	@Autowired
	private IHealthCoinSourceSettingSV healthCoinSourceSettingSV;

	@ApiOperation("健康币渠道列表展示")
	@GetMapping(value = "/healthCoinSourceSetting/list")
	@ResponseBody
	public APIResponse<PageInfo<HealthCoinSourceSettingEntity>> listHealthCoinSourceSetting(
			@ApiParam(name = "pageNo", value = "页码", required = false)
			@RequestParam(name = "pageNo", required = true, defaultValue = "1")
					int pageNo,
			@ApiParam(name = "limit", value = "每页数量", required = false)
			@RequestParam(name = "limit", required = false, defaultValue = "10")
					int limit,
			@ApiParam(name = "model", value = "健康币获取渠道设置实体", required = false)
					HealthCoinSourceSettingParam model) {
		PageHelper.startPage(pageNo,limit,"create_time desc");
		return successPage(healthCoinSourceSettingSV.selectList(model.covert()));
	}

	@ApiOperation("健康币渠道设置新增")
	@PostMapping(value = "/healthCoinSourceSetting/add")
	@ResponseBody
	public APIResponse insertHealthCoinSourceSetting(
			@RequestBody
			@Validated(value = {HealthCoinSourceSettingParam.Add.class})
					HealthCoinSourceSettingParam healthCoinSourceSetting) {
		Long userId = ShiroUtil.getLoginId();
		healthCoinSourceSetting.setCreateSb(userId);
		healthCoinSourceSetting.setCreateTime(new Date());
		healthCoinSourceSetting.setCreateSb(userId);
		long flag = healthCoinSourceSettingSV.insert(healthCoinSourceSetting.covert());
		if (flag > 0) {
			return successData(healthCoinSourceSetting.getId());
		}
		return failMsg("健康币渠道新增失败!");
	}

	@ApiOperation("健康币渠道设置修改")
	@PostMapping(value = "/healthCoinSourceSetting/update")
	@ResponseBody
	public APIResponse updateHealthCoinSourceSetting(
			@RequestBody @Validated(value = {HealthCoinSourceSettingParam.Update.class})
					HealthCoinSourceSettingParam healthCoinSourceSetting) {
		int flag = healthCoinSourceSettingSV.updateById(healthCoinSourceSetting.covert());
		if (flag > 0 ) {
			return successData(healthCoinSourceSetting.getId());
		}
		return failMsg("健康币渠道设置修改失败!");
	}

	@ApiOperation("健康币渠道设置删除")
	@PostMapping(value = "/healthCoinSourceSetting/delete")
	@ResponseBody
	public APIResponse deleteHealthCoinSourceSetting(@ApiParam(name = "hospIds", value = "医院ids数组", required = true) @RequestBody Long[] ids) {
		if (StringUtils.isEmpty(ids)) {
			throw new BusinessException("请至少选择一个");
		}
		int rows = 0;
		rows = healthCoinSourceSettingSV.deleteByIds(ids);
		if (rows > 0) {
			return APIResponse.success("健康币渠道设置删除成功", rows);
		}
		return failMsg("健康币渠道设置删除失败!");
	}

}
